package com.basic.modules.nykf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.common.utils.*;
import com.basic.modules.nykf.dao.CustomerComplaintRegistrationDao;
import com.basic.modules.nykf.dao.FileNewDao;
import com.basic.modules.nykf.entity.CustomerComplaintRegistrationEntity;
import com.basic.modules.nykf.entity.FileNewEntity;
import com.basic.modules.nykf.service.CustomerComplaintRegistrationService;
import com.basic.modules.oss.cloud.OSSFactory;
import com.basic.modules.sys.dao.SysFileDao;
import com.basic.modules.sys.entity.SysFileEntity;
import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;


@Service
public class CustomerComplaintRegistrationServiceImpl extends ServiceImpl<CustomerComplaintRegistrationDao, CustomerComplaintRegistrationEntity>
        implements CustomerComplaintRegistrationService {

    @Autowired
    private CustomerComplaintRegistrationDao customerComplaintRegistrationDao;

    @Autowired
    private FileNewDao fileNewDao;

    @Autowired
    private SysFileDao sysFileDao;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageUtils getComplaintSuggestionsList(ParamsModel<CustomerComplaintRegistrationEntity> params) {
        IPage<CustomerComplaintRegistrationEntity> page = new Query<CustomerComplaintRegistrationEntity>().getPage(BeanUtil.beanToMap(params.getPage()));
        IPage<CustomerComplaintRegistrationEntity> customerComplaintRegistrationList = customerComplaintRegistrationDao.getComplaintSuggestionsList(page, params.getCondition());
        return new PageUtils(customerComplaintRegistrationList);
    }

    /**
     * 新增投诉建议
     *
     * @param entity
     * @return
     */
    @Override
    public R addComplaintSuggestions(CustomerComplaintRegistrationEntity entity) {
        //获取当前用户id
        Long userId = ShiroUtils.getUserId();
        SysUserEntity user = sysUserService.getById(userId);
        String flowerName = user.getFlowerName();
        entity.setCjr(flowerName);
        entity.setDjsj(new Date());
        this.save(entity);
        return R.ok();
    }

    /**
     * 修改投诉建议
     *
     * @param entity
     * @return
     */
    @Override
    public R editCustomerComplaintRegistrationEntity(CustomerComplaintRegistrationEntity entity) {
        if (entity != null) {
            this.updateById(entity);
            return R.ok();
        } else {
            return R.error("修改失败");
        }
    }


    @Transactional
    @Override
    public R saved(String tsqd, String tskhdjqd, Date tswtfssj, String tskhdjqm, String wgwtfby,
                         String wgwtfbe, String zdclry, String gjc, String wtms, MultipartFile[] files) throws IOException {
        Long userId = ShiroUtils.getUserId();
        SysUserEntity user = sysUserService.getById(userId);
        String flowerName = user.getFlowerName();
        CustomerComplaintRegistrationEntity customerComplaintRegistrationEntity = new CustomerComplaintRegistrationEntity();
        customerComplaintRegistrationEntity.setDjsj(new Date());
        customerComplaintRegistrationEntity.setTskhdjqdVal(tskhdjqd);
        customerComplaintRegistrationEntity.setTskhdjqdName(tskhdjqd);
        customerComplaintRegistrationEntity.setTsqdName(tsqd);
        customerComplaintRegistrationEntity.setTsqdVal(tsqd);
        customerComplaintRegistrationEntity.setTskhdjqm(tskhdjqm);
        customerComplaintRegistrationEntity.setTswtfssj(tswtfssj);
        customerComplaintRegistrationEntity.setGjc(gjc);
        customerComplaintRegistrationEntity.setWtms(wtms);
        customerComplaintRegistrationEntity.setWgwtfbyVal(wgwtfby);
        customerComplaintRegistrationEntity.setWgwtfbyName(wgwtfby);
        customerComplaintRegistrationEntity.setWgwtfbeVal(wgwtfbe);
        customerComplaintRegistrationEntity.setWgwtfbeName(wgwtfbe);
        customerComplaintRegistrationEntity.setZdclry(zdclry);
        customerComplaintRegistrationEntity.setCjr(flowerName);
        customerComplaintRegistrationDao.insert(customerComplaintRegistrationEntity);
        Long complaintId = customerComplaintRegistrationEntity.getComplaintId(); // 获取生成的主键
        System.out.println("Generated ID: " + complaintId);

        if(files != null && files.length > 0){
            // 处理文件逻辑 两张表 sys_file 和 n_ykf_file
            for (MultipartFile file : files) {
                //sys_file
                SysFileEntity sysFileEntity = new SysFileEntity();
                String fileName = file.getOriginalFilename();
                long size = file.getSize();
                String suffix = file.getOriginalFilename().substring(fileName.lastIndexOf("."));
                String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix, "tsjy");
                sysFileEntity.setFileName(fileName);
                sysFileEntity.setFileType(suffix);
                sysFileEntity.setFileUrl(url);
                sysFileEntity.setFileSize(BigDecimal.valueOf(size));
                sysFileEntity.setCreateUserId(userId);
                sysFileDao.insert(sysFileEntity);
                //n_ykf_file
                Long fileId = sysFileEntity.getFileId();
                FileNewEntity fileNewEntity = new FileNewEntity();
                fileNewEntity.setGlId(complaintId);
                fileNewEntity.setMklx("tsjy");
                fileNewEntity.setCreateUserId(userId);
                fileNewEntity.setFileId(fileId);
                fileNewDao.insert(fileNewEntity);
            }
            return R.ok("新增投诉加附件");
        }
        return R.ok("新增投诉不加附件");
    }
}
